package com.fcscs.cservice.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fcscs.core.domain.PropertyEntity;
import com.fcscs.core.util.CommonUtil;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.cservice.domain.RestaurantPicture;
import com.fcscs.cservice.service.BookingManager;
import com.fcscs.cservice.service.RestaurantManager;
import com.fcscs.cservice.service.RestaurantPictureManager;
import com.fcscs.share.domain.nonexport.RCAttachment;
import com.fcscs.share.domain.nonexport.WsSession;
import com.fcscs.share.service.MiscDBManager;
import com.fcscs.share.service.WsSessionManager;

@Controller
public class WebServiceServletController {

	@Autowired
	MiscDBManager miscMgr;

	@Autowired
	private WsSessionManager wsSessionManager;

	@Autowired
	RestaurantManager restMgr;

	private boolean isSessionValid(long userId, String wsSessionId) {
		WsSession session = wsSessionManager.getSession(userId);
		if (session != null && session.getSessionId() != null && session.getSessionId().equals(wsSessionId)) {
			CommonUtil
					.setWsSession(session.getUserId(), session.getUsername(), session.getOrgId(), session.getPropId());
			return true;
		} else {
			return false;
		}
	}

	private void isLegalAccess(PropertyEntity entity) {
		long orgId = CommonUtil.getWsSessionOrgId();
		if (entity!=null && orgId != entity.getOrgId()) {
			throw new RuntimeException("Illegal Access");
		}
	}

	@RequestMapping("/ws/getRestaurantLogo.do")
	public void getRestaurantLogo(@RequestParam("fcsUserId") long userId,
			@RequestParam("wsSessionId") String sessionId, @RequestParam("restaurantId") long restaurantId,
			HttpServletRequest request, HttpServletResponse resp) throws IOException {

		if (!isSessionValid(userId, sessionId)) {
			resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
			return;
		}

		Restaurant pic = restMgr.get(restaurantId);
		isLegalAccess(pic);

		if (pic.getLogo() == null) {
			resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
		} else {
			resp.setContentType("image/png");
			resp.getOutputStream().write(pic.getLogo());
			resp.getOutputStream().flush();
			resp.setStatus(HttpServletResponse.SC_OK);
		}

	}

	@Autowired
	RestaurantPictureManager restPicMgr;

	@RequestMapping("/ws/getRestaurantPicture.do")
	public void getRestaurantPhoto(@RequestParam("fcsUserId") long userId,
			@RequestParam("wsSessionId") String sessionId, @RequestParam("pictureId") long photoId,
			HttpServletRequest request, HttpServletResponse resp) throws IOException {

		if (!isSessionValid(userId, sessionId)) {
			resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
			return;
		}

		RestaurantPicture pic = restPicMgr.get(photoId);
		isLegalAccess(pic);

		resp.setContentType("image/png");
		resp.getOutputStream().write(pic.getPic());
		resp.getOutputStream().flush();
		resp.setStatus(HttpServletResponse.SC_OK);

	}

	@RequestMapping("/ws/getRCSignature.do")
	public void getRCSignature(@RequestParam("fcsUserId") long userId, @RequestParam("wsSessionId") String sessionId,
			@RequestParam("rcId") long rcId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		if (!isSessionValid(userId, sessionId)) {
			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
			return;
		}

		RCAttachment att = miscMgr.getRCAttachment(rcId);
		isLegalAccess(att);
		if (att == null) {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		} else {
			response.getOutputStream().write(att.getSignature());
			response.getOutputStream().flush();
			response.setStatus(HttpServletResponse.SC_OK);
		}

	}

	@RequestMapping("/ws/getRCInitial.do")
	public void getRCInitial(@RequestParam("fcsUserId") long userId, @RequestParam("wsSessionId") String sessionId,
			@RequestParam("rcId") long rcId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		if (!isSessionValid(userId, sessionId)) {
			response.setStatus(HttpServletResponse.SC_FORBIDDEN);
			return;
		}

		RCAttachment att = miscMgr.getRCAttachment(rcId);
		isLegalAccess(att);
		if (att == null) {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		} else {
			response.getOutputStream().write(att.getInitial());
			response.getOutputStream().flush();
			response.setStatus(HttpServletResponse.SC_OK);
		}

	}

	@Autowired
	BookingManager bookMgr;
	@Value("/dummy/country.xlsx")
	Resource dummyCountryFile;

	@RequestMapping("/ws/seedCountry.do")
	public void seedCountryData(@RequestParam("propId") long propId,
			HttpServletRequest request, HttpServletResponse respons) throws IOException {
		File file = dummyCountryFile.getFile();
		bookMgr.seedCountry(propId, file.getAbsolutePath());
	}
}
